package com.yjj.im.ui;

import android.annotation.SuppressLint;
import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.yjj.im.R;
import com.yjj.im.bean.DataBean;

import java.util.ArrayList;


public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

    private Context context;
    public ArrayList<DataBean> data;
    private onItemClickListener listener;

    //添加接口
    public interface onItemClickListener{
        void onItemClick(int position);
    }

    //创建构造方法
    public void setonItemClickListener(onItemClickListener listener) {
        this.listener = listener;
    }


    public MyAdapter(Context context, ArrayList<DataBean> data){
        this.context=context;
        this.data=data;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item, parent, false);

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        String company = MessageActivity.compayKeyList.get(data.get(position).company);
        String company1 = MessageActivity.compayKeyList.get(data.get(position).company2);
        String product = MessageActivity.compayKeyList.get(data.get(position).product);
        holder.title.setText(MessageActivity.clientName);
        holder.tvItem1.setText(company);
        holder.tvItem2.setText(company1);
        holder.tvItem3.setText(product);
        holder.tvItem4.setText(data.get(position).weight2+"");
        holder.tvItem5.setText(data.get(position).weight1+"");
        holder.tvItem6.setText(data.get(position).weight1-data.get(position).weight2+"");
        holder.tvItem7.setText(data.get(position).car);
        holder.tvItem8.setText(data.get(position).company);
        holder.tvItem9.setText(data.get(position).note);
        holder.tvItem10.setText(data.get(position).cserial);
        holder.tvItem11.setText(data.get(position).ctime);
        holder.tvItem12.setText(data.get(position).ctime2);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @SuppressLint("SuspiciousIndentation")
            @Override
            public void onClick(View view) {
                if(listener != null) {
                    int position = holder.getAdapterPosition();
                    if(position != RecyclerView.NO_POSITION) //点击的位置是否为空
                        listener.onItemClick(position);
                }
            }
        });


    }

    @Override
    public int getItemCount()
    {
        return data.size();
    }


    public class ViewHolder extends RecyclerView.ViewHolder {
        public TextView tvItem1,tvItem2,tvItem3,tvItem4,tvItem5,tvItem6,tvItem7,tvItem8,tvItem9,tvItem10,tvItem11,title,tvItem12;
        public ViewHolder(View itemView) {
            super(itemView);
            title=(TextView)itemView.findViewById(R.id.title) ;
            tvItem1=(TextView)itemView.findViewById(R.id.value_item) ;
            tvItem2=(TextView)itemView.findViewById(R.id.value_item1) ;
            tvItem3=(TextView)itemView.findViewById(R.id.value_item2) ;
            tvItem4=(TextView)itemView.findViewById(R.id.value_item3) ;
            tvItem5=(TextView)itemView.findViewById(R.id.value_item4) ;
            tvItem6=(TextView)itemView.findViewById(R.id.value_item5) ;
            tvItem7=(TextView)itemView.findViewById(R.id.value_item6) ;
            tvItem8=(TextView)itemView.findViewById(R.id.value_item7) ;
            tvItem9=(TextView)itemView.findViewById(R.id.value_item8) ;
            tvItem10=(TextView)itemView.findViewById(R.id.value_item9) ;
            tvItem11=(TextView)itemView.findViewById(R.id.value_item10) ;
            tvItem12=(TextView)itemView.findViewById(R.id.value_item11) ;

        }
    }

}
